<template>
  <el-card>
    <div slot="header"
         class="clearfix">
      <!-- 面包屑内容 -->
      <el-breadcrumb>
        <el-breadcrumb-item to="/home"><i class="el-icon-house"></i>首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 主体内容 -->
    <!-- 搜索与添加区域 -->
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input placeholder="请输入内容"
                  clearable
                  v-model="queryInfo.query"
                  @clear="onSearchCancel">
          <el-button icon="el-icon-search"
                     slot="append"
                     @click="onSearchSubmit">
          </el-button>
        </el-input>
      </el-col>
      <el-col :offset="12"
              :span="4">
        <el-button type="primary"
                   @click="$router.push('/goods/add')">添加商品</el-button>
      </el-col>
    </el-row>

    <!-- 用户列表 -->
    <!-- data 表格数据 -->
    <el-table :data="goodslist"
              border
              stripe>
      <!-- 索引页 -->
      <el-table-column type="index"></el-table-column>
      <!-- label 文字， prop 显示字段 -->
      <el-table-column prop="goods_name"
                       label="商品名称">
      </el-table-column>
      <el-table-column prop="goods_price"
                       label="商品价格(元)">
      </el-table-column>
      <el-table-column prop="goods_weight"
                       label="商品重量">
      </el-table-column>
      <el-table-column prop="add_time"
                       label="创建时间">
        <template slot-scope="scope">
          {{scope.row.add_time | dateFormat}}
        </template>
      </el-table-column>
      <el-table-column label="操作"
                       width="180px">
        <template slot-scope="scope">
          <!-- 修改按钮 -->
          <el-button type="primary"
                     icon="el-icon-edit"
                     size="mini">
          </el-button>
          <!-- 删除按钮 -->
          <el-button type="danger"
                     icon="el-icon-delete"
                     size="mini"
                     @click="removeById(scope.row.goods_id)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="queryInfo.pagenum"
                   :page-sizes="[1, 2, 5, 10]"
                   :page-size="queryInfo.pagesize"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="total">
    </el-pagination>

  </el-card>
</template>

<script>
export default {
  data () {
    return {
      // 用户列表参数
      queryInfo: {
        query: '',
        // 页数
        pagenum: 1,
        // 每页数据
        pagesize: 2
      },
      // 列表总数
      goodslist: [],
      // 总数
      total: 0
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取数据
    async getGoodsList () {
      // 发送请求
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      // 错误处理
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      // 成功处理
      this.goodslist = res.data.goods
      this.total = res.data.total
      // 成功之后要去看输出内容,确保输出正确
      console.log(res)
    },
    // 监听 pagesize 改变的事件
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听 pagenum（页码值） 改变的事件
    handleCurrentChange (newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 条件搜索提交
    onSearchSubmit () {
      this.queryInfo.pagenum = 1
      this.getGoodsList()
    },
    // 条件搜索重置
    onSearchCancel () {
      this.queryInfo.pagenum = 1
      this.queryInfo.pagesize = 10
      this.getGoodsList()
    },
    // 根据id删除用户信息
    async removeById (id) {
      // 弹出询问框
      try {
        await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // 确认删除
        // 发请求
        const { data: res } = await this.$http.delete(`goods/${id}`)
        // 错误处理
        if (res.meta.status !== 200) {
          return this.$message.error('删除失败')
        }
        this.$message.success('删除成功')
        // 判断是否是每页最后一条数据和第一页
        if (this.goodslist.length === 1 && this.queryInfo.pagenum !== 1) {
          this.queryInfo.pagenum--
        }
        this.getGoodsList()
      } catch (e) {
        this.$message.info('已取消删除')
      }
    }

  }
}
</script>

<style lang="less" scoped>
.el-row {
  margin-bottom: 15px;
}
</style>
